<?php
/**
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JLoader::register('ExercisesHelper', JPATH_COMPONENT.'/helpers/exercises.php');

class ExercisesViewResult extends JViewLegacy
{
	protected $form;
	protected $item;
	protected $state;

	/**
	 * Display the view
	 */
	public function display($tpl = null)
	{
		// Initialise variables.
		$this->form	= $this->get('Form');
		$this->item	= $this->get('Item');
		$this->state	= $this->get('State');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->addToolbar();
                JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
                JHtml::_('exercise.stylesheet');
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addToolbar()
	{
		JRequest::setVar('hidemainmenu', true);

		$user		= JFactory::getUser();
		$isNew		= ($this->item->id == 0);
		$canDo		= ExercisesHelper::getActions();
		JToolBarHelper::title($isNew ? JText::_('COM_EXERCISES_MANAGER_RESULT_NEW') : JText::_('COM_EXERCISES_MANAGER_RESULT_EDIT'), 'exercises-results.png');

		// If not checked out, can save the item.
		if (($canDo->get('core.edit')||$canDo->get('core.create'))) {
			JToolBarHelper::apply('result.apply');
			JToolBarHelper::save('result.save');
		}
		
		if (empty($this->item->id))  {
			JToolBarHelper::cancel('result.cancel');
		} else {
			JToolBarHelper::cancel('result.cancel', 'JTOOLBAR_CLOSE');
		}

		JToolBarHelper::divider();
	}
}
